<?php
namespace app\pgb\controller;
class Ordergoods extends Base
{
    //列表
    public function lists(){
        if(is_post()){
            $d = $this->post;
            $map = [];
            if($d['keyword']) $map[] = ['and','b.username|b.phone|a.order_no','like',"%{$d['keyword']}%"];
            if($d['sh_keyword']) $map[] = ['and','a.sh_name|a.sh_phone|sh_address','like',"%{$d['sh_keyword']}%"];
            if($d['title'])   $map[] = ['and','a.title','like',"%{$d['title']}%"];
            if($d['refund_status']!='') $map[] = ['and','a.refund_status','=',$d['refund_status']];
            if($d['pay_status']!='') $map[] = ['and','a.pay_status','=',$d['pay_status']];
            if($d['fahuo_status']!='') $map[] = ['and','a.fahuo_status','=',$d['fahuo_status']];
            if($d['min_time']!='') $map[] = ['and','a.create_time','>=',strtotime($d['min_time'])];
            if($d['max_time']!='') $map[] = ['and','a.create_time','<=',strtotime($d['max_time'])];
            $w  = where($map);
            $sql = "select a.*,group_concat(concat_ws('',title,'(',goods_price_title,')','X',nums) separator '<br/>') as goods_title,b.username from order_goods as a
                          left join user b on a.user_id=b.id $w[0]
                          group by a.order_no order by a.id desc".limit($d['page'],$d['limit']);
            $rs = $this->db->query($sql, $w[1],'',true);
            //下载,圆通模板
            if(isset($d['is_down']) && $d['is_down']){
                $sql = "select a.*,b.username from order_goods as a
                              left join user b on a.user_id=b.id $w[0]
                              order by a.id desc";
                $rs_down = $this->db->query($sql, $w[1]);
                foreach ($rs_down as $k => $v) {
                    $rs_down[$k]['goods_price_title'] = $v['title'] . ' ' . $v['goods_price_title'];
                    $rs_down[$k]['fahuo_name'] = $this->sys['fahuo_name'];
                    $rs_down[$k]['fahuo_phone'] = $this->sys['fahuo_phone'];
                    $rs_down[$k]['fahuo_dizhi'] = $this->sys['fahuo_dizhi'];
                    $rs_down[$k]['fahuo_youbian'] = $this->sys['fahuo_youbian'];
                    $rs_down[$k]['daishouhuokuan'] = '';
                    $rs_down[$k]['shoujianrenyuobian'] = '';
                    $rs_down[$k]['shoujianrenguhua'] = '';
                    $rs_down[$k]['baojiajine'] = '';
                    $rs_down[$k]['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
                }
                $title = ['物流单号','会员账号','商品名称','数量','单价','总价','收件人姓名','收件人地址','收件人手机号','发件人姓名','发件人电话','发件人地址','发件人邮编','代收货款','备注','收件人邮编','收件人固话','保价金额','日期'];
                $fields= ['kd_no','username','goods_price_title','nums','goods_price_price','total_money','sh_name','sh_address','sh_phone','fahuo_name','fahuo_phone','fahuo_dizhi','fahuo_youbian','daishouhuokuan','kd_note','shoujianrenyuobian','shoujianrenguhua','baojiajine','create_time'];
                $file = com_csv($rs_down,$title,$fields,'ordergoods');
                cookie('js_url',$file);
            }
            if($rs){
              $rule = [
                  'pay_status' => function($v){return c("type.pay_status.{$v}");},
                  'fahuo_status' => function($v){return c("type.fahuo_status.{$v}");},
                  'refund_status' => function($v){return c("type.refund_status.{$v}");},
                  'sh_status' => function($v){return c("type.sh_status.{$v}");},
                  'create_time' => 'com_time_format',
              ];
              $data = com_format($rs[0],$rule);
              suc($data,$rs[1]);
            }else{
                suc([],0);
            }
        }
        $this->fetch();
    }

    //删除
    public function del(){
      if(is_post()){
        $order_no = $this->post["order_no"];
        $list = $this->db->query("select * from order_goods where order_no=?",[$order_no]);
        $rs = $this->db->delete("order_goods",$order_no,'order_no');
        $msg = "删除了订单,内容为:" . json_encode($list,JSON_UNESCAPED_UNICODE);
        //添加日志
        m('Log','xcom')->record($this->userinfo["id"], $msg,'admin_user');
        if($rs){
            cookie('js_msg','删除成功');
            cookie('js_function','close|reload');
            return suc();
        }else{
            return err("删除失败");
        }
      }

    }

    //取消订单
    public function cancel(){
        $order_no = $this->post("order_no");
        $sql = "select * from order_goods where order_no = ? order by id desc";
        $list = $this->db->query($sql,[$order_no]);
        $m = m('orderGoods','c');
        foreach ($list as $k => $v) {
            $rs = $m->edit_refund($v['id'],1,$this->userinfo['id']);
            if(!$rs){
                return err($m->getError());
            }
        }
        cookie('js_msg','取消成功');
        cookie('js_function','close|reload');
        return suc();
    }


    //发货
    public function detail(){
        $order_no = $this->get("order_no");
        $sql = "select * from order_goods where order_no = ? order by id desc";
        $rs = $this->db->query($sql,[$order_no]);
        $user = $this->db->query("select * from user where id = ?",[$rs[0]['user_id']],1);
        $no_info = $this->db->query("select * from order_goods_no where order_no =?",[$order_no],1);
        $this->fetch(["list" => $rs,'user'=>$user,'no_info'=>$no_info]);
    }

    #批量删除
    function delall(){
      if(is_post()){
            $d = $this->post;
            $ids = $d['id'];
            foreach($ids as $id){
                $info = $this->db->find('order_goods',$id);
                if($info){
                    //添加日志
                    $list = $this->db->query("select * from order_goods where order_no=?",[$info['order_no']]);
                    $msg = "删除了订单,内容为:" . json_encode($list,JSON_UNESCAPED_UNICODE);
                    m('Log','xcom')->record($this->userinfo["id"], $msg,'admin_user');
                    $this->db->delete('order_goods',$info['order_no'],'order_no');
                }
            }
            cookie('js_msg','删除成功');
            cookie('js_function','close|reload');
            return suc();
      }
    }

    public function allDownOrder(){
        //快递公司列表
        $elist = $this->db->query("select * from express where status = ?",[0]);
        $this->fetch(['elist'=>$elist],'',false);
    }

    // //修改订单金额
    // public function edit(){
    //     if(is_post()){
    //         $d = $this->post;
    //         $msg = "修改了订单价格,订单编号:" . $d['order_no'];
    //         m('Log','xcom')->record($this->userinfo["id"], $msg,'admin_user');
    //         $rs = $this->db->update('order_goods',['order_total_money'=>$d['order_total_money']],['order_no=?',[$d['order_no']]]);
    //         if($rs){
    //             cookie('js_msg','修改成功');
    //             cookie('js_function','close|reload');
    //             return suc();
    //         }else{
    //             return err("修改失败");
    //         }
    //     }
    //     $order_no = $this->get("order_no");
    //     $sql = "select * from order_goods where order_no = ? order by id desc";
    //     $rs = $this->db->query($sql,[$order_no],1);
    //     $this->fetch(["info" => $rs]);
    // }
}
